package com.mobile.paperazi;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.mobile.paperazi.data.Constants;
import com.mobile.paperazi.data.Content;

public class OptionPopupActivity extends Activity {
	private ListView lstView;
	private ArrayList<Content> list;
	Dialog d;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		Intent intent = getIntent();
		list = intent.getParcelableArrayListExtra(Constants.CONTENT);
		LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = li.inflate(R.layout.listview, null, false);
		setContentView(v);
		lstView = (ListView)v.findViewById(android.R.id.list);
		ArrayAdapter<Content> adapter = new OptionPopupArrayAdapter(this, list);
		lstView.setAdapter(adapter);
		lstView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Content content = list.get(arg2);
				if(content.getType().equals(Constants.WEB_PAGE) || content.getType().equals(Constants.VIDEO)){
					Intent intent = new Intent(OptionPopupActivity.this, WebActivity.class);
					intent.putExtra(Constants.CONTENT, content);
					intent.putExtra(Constants.IS_PAPERAZI, true);
					startActivity(intent);
				}
				else if(content.getType().equals(Constants.CALL)){
					 Intent callIntent = new Intent(Intent.ACTION_CALL); 
					 String str = content.getDescription().trim();
			         callIntent.setData(Uri.parse("tel:"+ str));          
			         startActivity(callIntent);
				}
				else if(content.getType().equals(Constants.SMS)){
					String str = content.getDescription().trim();
					if(str != null && !str.equals("")){
						String[] arr = str.split(",");
						Uri smsUri = Uri.parse("sms:" + arr[0]);
						Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
						intent.putExtra(Constants.SMS_BODY, arr[1]);
						startActivity(intent);
					}
					
				}
				
			}
		});
		Button btn = (Button)v.findViewById(R.id.btnCancel);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}
	
}
